﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using System.Linq;

namespace TruyenAudio
{
    public class ListItem : List<Item>
    {
        public static string TheLoai = "";
        public static string TenTruyen = "";
        public static string Taptruyen = "";

        public static string LinkTruyen = "";

        public ListItem()
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            if (!store.FileExists("CSDLTruyen.sdf"))
            {
                MainPage.MoveReferenceDatabase();
            }

            BuildTheLoai();
            BuildDanhSach(TheLoai);
            BuilTapTruyen(TenTruyen);
        }

        private string IMG_PATH = "../images/Theloaikhac/";

        public ObservableCollection<Item> DataTheLoai { get; set; }
        public ObservableCollection<Item> DataDanhSach { get; set; }
        public ObservableCollection<Item> DataTapTruyen { get; set; }
        public ObservableCollection<Item> BuildTheLoai()
        {
            DataTheLoai = new ObservableCollection<Item>();

            List<TheLoai> list = DBhelper.GetTheLoai();
            for (int i = 0; i < list.Count; i++)
            {
                DataTheLoai.Add(new Item(list[i].TenTheLoai, IMG_PATH + list[i].Hinh + ".png"));
            }
            return DataTheLoai;
        }
        public ObservableCollection<Item> BuildDanhSach(string str)
        {
            DataDanhSach = new ObservableCollection<Item>();
            List<TRUYEN> list = DBhelper.GetDanhSachTruyen(str);
            for (int i = 0; i < list.Count; i++)
            {
                DataDanhSach.Add(new Item(list[i].TenTruyen, IMG_PATH + list[i].Hinh + ".png"));
            }
            return DataDanhSach;
        }
        /************************************BUIL Tap Truyen*************************************************************/
        public ObservableCollection<Item> BuilTapTruyen(string TenTruyen)
        {
            DataTapTruyen = new ObservableCollection<Item>();
            if (TheLoai == "Kiếm Hiệp")
            {
                using (var context = new CSDLTruyenContext(CSDLTruyenContext.ConnectionString))
                {
                    var truyen = (from tr in context.TRUYEN from tinhyeu in context.KIEMHIEP where tinhyeu.MaTryen == tr.MaTruyen && tr.TenTruyen == TenTruyen select new { tinhyeu.TenTruyen, tinhyeu.Link, tr.Hinh }).ToList();
                    for (int i = truyen.Count - 1; i >= 0; i--)
                    {
                        DataTapTruyen.Add(new Item(truyen[i].TenTruyen, IMG_PATH + truyen[i].Hinh + ".png", truyen[i].Link));
                    }

                }

            }
            else if (TheLoai == "Tình Cảm")
            {
                IMG_PATH = "../images/tinhyeu/";
                using (var context = new CSDLTruyenContext(CSDLTruyenContext.ConnectionString))
                {
                    var truyen = (from tr in context.TRUYEN from tinhyeu in context.TINHYEU where tinhyeu.MaTryen == tr.MaTruyen && tr.TenTruyen == TenTruyen select new { tinhyeu.TenTruyen, tinhyeu.Link, tr.Hinh }).ToList();
                    int k = 1;
                    for (int i = truyen.Count - 1; i >= 0; i--)
                    {

                        DataTapTruyen.Add(new Item(truyen[i].TenTruyen, IMG_PATH + k.ToString() + ".png", truyen[i].Link));
                        k++;
                        if (k >= 22)
                        {
                            k = 1;
                        }
                    }

                }

            }
            else
                if (TheLoai == "Sách Nói")
                {
                    using (var context = new CSDLTruyenContext(CSDLTruyenContext.ConnectionString))
                    {
                        var truyen = (from tr in context.TRUYEN from tinhyeu in context.SACHNOI where tinhyeu.MaTryen == tr.MaTruyen && tr.TenTruyen == TenTruyen select new { tinhyeu.TenTruyen, tinhyeu.Link, tr.Hinh }).ToList();
                        for (int i = truyen.Count - 1; i >= 0; i--)
                        {
                            DataTapTruyen.Add(new Item(truyen[i].TenTruyen, IMG_PATH + truyen[i].Hinh + ".png", truyen[i].Link));
                        }

                    }

                }
                else
                    if (TheLoai == "Thiếu Nhi")
                    {
                        using (var context = new CSDLTruyenContext(CSDLTruyenContext.ConnectionString))
                        {
                            var truyen = (from tr in context.TRUYEN from tinhyeu in context.THIEUNHI where tinhyeu.MaTryen == tr.MaTruyen && tr.TenTruyen == TenTruyen select new { tinhyeu.TenTruyen, tinhyeu.Link, tr.Hinh }).ToList();
                            for (int i = truyen.Count - 1; i >= 0; i--)
                            {
                                DataTapTruyen.Add(new Item(truyen[i].TenTruyen, IMG_PATH + truyen[i].Hinh + ".png", truyen[i].Link));
                            }

                        }

                    }
                    else
                        if (TheLoai == "Kinh Điển")
                        {
                            using (var context = new CSDLTruyenContext(CSDLTruyenContext.ConnectionString))
                            {
                                var truyen = (from tr in context.TRUYEN from tinhyeu in context.KINHDIEN where tinhyeu.MaTryen == tr.MaTruyen && tr.TenTruyen == TenTruyen select new { tinhyeu.TenTruyen, tinhyeu.Link, tr.Hinh }).ToList();
                                for (int i = truyen.Count - 1; i >= 0; i--)
                                {
                                    DataTapTruyen.Add(new Item(truyen[i].TenTruyen, IMG_PATH + truyen[i].Hinh + ".png", truyen[i].Link));
                                }

                            }

                        }
                        else
                            if (TheLoai == "Tiểu Thuyết")
                            {
                                using (var context = new CSDLTruyenContext(CSDLTruyenContext.ConnectionString))
                                {
                                    var truyen = (from tr in context.TRUYEN from tinhyeu in context.TIEUTHUYET where tinhyeu.MaTryen == tr.MaTruyen && tr.TenTruyen == TenTruyen select new { tinhyeu.TenTruyen, tinhyeu.Link, tr.Hinh }).ToList();
                                    for (int i = truyen.Count - 1; i >= 0; i--)
                                    {
                                        DataTapTruyen.Add(new Item(truyen[i].TenTruyen, IMG_PATH + truyen[i].Hinh + ".png", truyen[i].Link));
                                    }

                                }

                            }
                            else
                                if (TheLoai == "Kinh Dị")
                                {
                                    using (var context = new CSDLTruyenContext(CSDLTruyenContext.ConnectionString))
                                    {
                                        var truyen = (from tr in context.TRUYEN from tinhyeu in context.KINHDI where tinhyeu.MaTryen == tr.MaTruyen && tr.TenTruyen == TenTruyen select new { tinhyeu.TenTruyen, tinhyeu.Link, tr.Hinh }).ToList();
                                        for (int i = truyen.Count - 1; i >= 0; i--)
                                        {
                                            DataTapTruyen.Add(new Item(truyen[i].TenTruyen, IMG_PATH + truyen[i].Hinh + ".png", truyen[i].Link));
                                        }

                                    }

                                }
                                else
                                    if (TheLoai == "Truyện Cười")
                                    {
                                        using (var context = new CSDLTruyenContext(CSDLTruyenContext.ConnectionString))
                                        {
                                            var truyen = (from tr in context.TRUYEN from tinhyeu in context.YNGHIA where tinhyeu.MaTryen == tr.MaTruyen && tr.TenTruyen == TenTruyen select new { tinhyeu.TenTruyen, tinhyeu.Link, tr.Hinh }).ToList();
                                            for (int i = truyen.Count - 1; i >= 0; i--)
                                            {
                                                DataTapTruyen.Add(new Item(truyen[i].TenTruyen, IMG_PATH + truyen[i].Hinh + ".png", truyen[i].Link));
                                            }

                                        }

                                    }

            return DataTapTruyen;
        }





    }
}
